package com.sxbang.bms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxbang.bms.base.result.ResponseCode;
import com.sxbang.bms.base.result.Results;
import com.sxbang.bms.dao.PermissionDao;
import com.sxbang.bms.dao.RoleDao;
import com.sxbang.bms.dao.RolePermissionDao;
import com.sxbang.bms.dto.RoleDto;
import com.sxbang.bms.model.RolePermission;
import com.sxbang.bms.model.SysPermission;
import com.sxbang.bms.model.SysRole;
import com.sxbang.bms.service.PermissionService;
import com.sxbang.bms.service.RoleService;
import com.sxbang.bms.util.TreeUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Override
    public Results<SysPermission> save(SysPermission sysPermission) {
        permissionDao.save(sysPermission);
        Results results = new Results();
        results.setCode(ResponseCode.SUCCESS.getCode());
        results.setMsg(ResponseCode.SUCCESS.getMessage());
        return results;
    }

    @Override
    public List<SysPermission> save(List<SysPermission> sysPermissions) {
        return null;
    }

    @Override
    public SysPermission getSysPermissionById(Integer id) {
        return permissionDao.get(id);
    }


    @Override
    @Transactional
    public Results<SysPermission> updateSysPermission(SysPermission e) {
        int res =  permissionDao.update(e);
        Results results = new Results();
        results.setCode(ResponseCode.SUCCESS.getCode());
        results.setMsg(ResponseCode.SUCCESS.getMessage());
        return results;
    }

    @Override
    public List<SysPermission> findAll() {
        return permissionDao.findAll();
    }


    @Override
    public List<SysPermission> list(Integer offset, Integer limit) {
        return permissionDao.listByPage(offset, limit);
    }



    @Override
    @Transactional
    public void delete(SysPermission e) {

        permissionDao.delete(e);
        RolePermission rolePermission = new RolePermission();
        rolePermission.setPermissionId(e.getId());
        rolePermissionDao.delete(rolePermission);
    }

    @Override
    @Transactional
    public void delete(Integer id) {

        permissionDao.deleteById(id);
        RolePermission rolePermission = new RolePermission();
        rolePermission.setPermissionId(id);
        rolePermissionDao.delete(rolePermission);
    }

    @Override
    public Long countAll() {
        return permissionDao.countAll();
    }

    @Override
    public Results<SysPermission> listByPage(Integer offset, Integer limit) {
        log.info(getClass().getName() + ".listByPage(" + offset + "," + limit + ")");
        return Results.success(permissionDao.countAll().intValue(), permissionDao.listByPage(offset, limit));
    }

    @Override
    public Results<SysPermission> listByName(String name) {
        return Results.success(0, permissionDao.listByName(name));
    }

    @Override
    public Results<SysPermission> listByNameAndPage(String name, Integer offset, Integer limit) {
        return Results.success(permissionDao.countByName(name).intValue(), permissionDao.listByNameAndPage(name, offset, limit));
    }

    @Override
    public Long countByName(String name) {
        return permissionDao.countByName(name);
    }

    @Override
    public Results<JSONArray> listAllPermission() {
        log.info(getClass().getName() + ".listAllPermission()");
        List datas = permissionDao.findAll();
        JSONArray array = new JSONArray();
        log.info(getClass().getName() + ".setPermissionsTree(?,?,?)");
        TreeUtils.setPermissionsTree(0, datas, array);
        return Results.success(array);
    }




    @Override
    public Results<SysPermission> listByRoleId(Integer roleId) {
        List<SysPermission> datas = permissionDao.listByRoleId(roleId);
        return Results.success(0, datas);
    }

    @Override
    public Results<SysPermission> listByUserId(Long userId) {
        List<SysPermission> datas = permissionDao.listByUserId(userId);
        return Results.success(0, datas);
    }

    public Results getMenu(Long userId) {
        List<SysPermission> datas = permissionDao.listByUserId(userId);
        datas = datas.stream().filter(p -> p.getType().equals(1)).collect(Collectors.toList());
        JSONArray array = new JSONArray();
        log.info(getClass().getName() + ".setPermissionsTree(?,?,?)");
        TreeUtils.setPermissionsTree(0, datas, array);
        return Results.success(array);
    }

    @Override
    public Results<SysPermission> getMenuAll() {
        List<SysPermission> datas = permissionDao.findAll();
        return Results.success(0, datas);
    }
}
